﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace DAL_Entity
{
    public static class HelpMethods
    {
        /// <summary>
        /// Copies all properties and overwrites the original
        /// </summary>
        /// <typeparam name="T">Any reference object</typeparam>
        /// <param name="target">Output object</param>
        /// <param name="source">Input object</param>
        /// <returns>Object with same type and all new proppierties</returns>
        public static T CopyProperties<T>(this T target, T source)
            where T : class
        {
            var type = target.GetType();

            var propperties = source.GetType().GetProperties();
            for (int i = 0; i < propperties.Length; i++)
            {
                propperties[i].SetValue(target, propperties[i].GetValue(source));
            }

            return target;
        }


        /// <summary>
        /// Copies all properties and overwrites the original
        /// </summary>
        /// <typeparam name="T">Any reference object</typeparam>
        /// <param name="target">Output object</param>
        /// <param name="source">Input object</param>
        /// <returns>Object with same type and all new proppierties</returns>
        public static T CopyNotNullProperties<T>(this T target, T source)
            where T : class
        {
            var type = target.GetType();

            var propperties = source.GetType().GetProperties();
            foreach (PropertyInfo t in propperties)
            {
                if (t.GetValue(source) != null)
                {
                    t.SetValue(target, t.GetValue(source));
                }
                
            }

            return target;
        }
    }
}
